-- This file is part of the Fantasy Grounds Open Foundation Ruleset project. 
-- For the latest information, see http://www.fantasygrounds.com/
--
-- Copyright 2008 SmiteWorks Ltd.
--
-- This file is provided under the Open Game License version 1.0a
-- Refer to the license.html file for the full license text
--
-- All producers of work derived from this material are advised to
-- familiarize themselves with the license, and to take special
-- care in providing the definition of Product Identity (as specified
-- by the OGL) in their products.
--
-- All material submitted to the Open Foundation Ruleset project must
-- contain this notice in a manner applicable to the source file type.


local dataclasshint = nil;


function onInit()
	registerMenuItem("Create Group", "insert", 5);
end

function onMenuSelection(selection)
	if selection == 5 then
		grouplist.createWindow().list.createSourcedWindow();
	end
end


function getDataClassHint()
	local r = dataclasshint;
	dataclasshint = nil;
	return r;
end

function setDataClassHint(class)
	dataclasshint = class;
end



-- Active entry processing

function getActiveEntry()
	for k, v in ipairs(initlist.getWindows()) do
		if v.active and v.active.isActive() then
			return v;
		end
	end
	
	return nil;
end

function requestActivation(entry)
	for k, v in ipairs(initlist.getWindows()) do
		if v.active then
			v.active.setActive(false);
		end
	end
	
	if entry.active then
		entry.active.setActive(true);
	end
end

function nextActor()
	local entry = initlist.getNextWindow(getActiveEntry());

	while entry and entry.getDatabaseNode().createChild("...hidden", "number").getValue() == 1 do
		entry = initlist.getNextWindow(entry);
	end
	
	if entry then
		requestActivation(entry);
	
		for k, v in ipairs(initlist.getWindows()) do
			v.effects.progressEffects(entry);
		end
	else
		nextRound();
	end
end

function nextRound()
	local entry = initlist.getNextWindow(nil);

	while entry and entry.getDatabaseNode().createChild("...hidden", "number").getValue() == 1 do
		entry = initlist.getNextWindow(entry);
	end
	
	if entry then
		requestActivation(entry);
		for k, v in ipairs(initlist.getWindows()) do
			v.effects.progressEffects(entry);
		end
	end
	
	roundcounter.setValue(roundcounter.getValue() + 1);
	
	for k, v in ipairs(initlist.getWindows()) do
		v.effects.progressEffects(nil);
	end
end
